package com.example.backend.service.impl;

import com.example.backend.entity.Customer;
import com.example.backend.repository.CustomerRepository;
import com.example.backend.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerRepository customerRepository;

    @Override
    public Customer save(Customer customer) {
        return customerRepository.save(customer);
    }

    @Override
    public List<Customer> findAll() {
        return customerRepository.findAll();
    }

    @Override
    public List<Customer> findByName(String name) {
        return customerRepository.findByName(name);
    }

    @Override
    public List<Customer> findByIdentity(String identity) {
        return customerRepository.findByIdentity(identity);
    }

    @Override
    public List<Customer> findByAge(Integer age) {
        return customerRepository.findByAge(age);
    }

    @Override
    public List<Customer> findByAddress(String address) {
        return customerRepository.findByAddress(address);
    }

    @Override
    public List<Customer> findByPhone(String phone) {
        return customerRepository.findByPhone(phone);
    }

    @Override
    public List<Customer> findById(Integer id) {
        if (!customerRepository.findById(id).isPresent()){
            return null;
        }
        else {
            Customer c =customerRepository.findById(id).get();
            return List.of(c);
        }
    }

    @Override
    public boolean findName(String name) {
        if (customerRepository.findByName(name).isEmpty()){
            return false;
        }
        return true;
    }

    @Override
    public boolean findNameAndId(Integer id, String name) {
        if (customerRepository.findByName(name).isEmpty()){
            return false;
        }
        else if (customerRepository.findByName(name).get(0).getId()==id){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public void delete(Integer id) {
        Customer customer = customerRepository.findById(id).get();
        customerRepository.delete(customer);
    }

    @Override
    public Customer update(Customer customer) {
        return customerRepository.save(customer);
    }
}
